package dao.fake;

import java.util.Set;

import interfaceDAO.IDAOPreference;
import domain.Preference;
import domain.User;

public class FakeDAOPreference implements IDAOPreference {

	@Override
	public Preference addPreference(User user, String alias, String address) {
		Preference pref = new Preference();
		pref.setUser(user);
		pref.setAlias(alias);
		pref.setAddress(address);
		Set<Preference> listPref = user.getAddressBook();
		listPref.add(pref);
		FakeDAOUser.saveUsers();
		return pref;
	}

	@Override
	public boolean deletePreference(User user, String alias) {
		Preference pref = getPreference(user, alias);
		if (pref != null) {
			user.getAddressBook().remove(pref);
			FakeDAOUser.saveUsers();
			return true;
		}
		return false;
	}

	@Override
	public Preference modifyPreference(User user, String alias, String address) {
		Preference pref = getPreference(user, alias);
		if (pref != null) {
			pref.setAddress(address);
			FakeDAOUser.saveUsers();
		}
		return pref;
	}

	@Override
	public Preference getPreference(User user, String alias) {
		for (Preference pref : user.getAddressBook()) {
			if (pref.getAlias().equals(alias)) {
				return pref;
			}
		}
		return null;
	}

}
